/*
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
 
 Create at 2:20:54 PM on Sep 27, 2013.

 */
package com.googlecode.contourplot;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import org.jfree.chart.axis.ColorBar;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.ContourPlot;
import org.jfree.chart.plot.CrosshairState;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.data.contour.ContourDataset;
import org.jfree.ui.RectangleEdge;

/**
 * @author lianyuy
 * 
 */
@SuppressWarnings("deprecation")
public class ContourShapePlot extends ContourPlot {
	private static final long serialVersionUID = 2549723868815209202L;
	private ContourRenderType type;

	public ContourShapePlot() {
		super();
	}

	public ContourShapePlot(ContourDataset dataset, ValueAxis domainAxis,
			ValueAxis rangeAxis, ColorBar colorBar, ContourRenderType type) {
		super(dataset, domainAxis, rangeAxis, colorBar);
		this.type = type;
	}

	@Override
	public void contourRenderer(Graphics2D g2, Rectangle2D dataArea,
			PlotRenderingInfo info, ContourPlot plot, ValueAxis horizontalAxis,
			ValueAxis verticalAxis, ColorBar colorBar, ContourDataset data,
			CrosshairState crosshairState) {

		if (!(data instanceof ContourPlotDataSeries)) {
			return;
		}

		Object antiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_OFF);

		int X0 = (int) dataArea.getX();
		int Y0 = (int) dataArea.getY();

		int width = (int) Math.round(dataArea.getBounds2D().getWidth());
		int height = (int) Math.round(dataArea.getBounds2D().getHeight());

		BufferedImage canvas = new BufferedImage(width + 1, height + 1,
				BufferedImage.TYPE_INT_ARGB);

		for (int i = 0; i <= width; i++) {
			for (int j = 0; j <= height; j++) {
				double x = horizontalAxis.java2DToValue(i, dataArea,
						RectangleEdge.BOTTOM);
				double y = verticalAxis.java2DToValue(j, dataArea,
						RectangleEdge.LEFT);
				double value = getPlotValue(x, y, (ContourPlotDataSeries) data);

				if (Double.isNaN(value))
					continue;

				int color = getColorByValue(value);

				switch (type) {
				case ContourGrayLineType:
					// TODO
					break;
				case ContourColorLineType:
					// TODO
					break;
				case ColorRenderType:
					canvas.setRGB(i, j, color);
					break;
				}

			}
		}

		g2.drawImage(canvas, null, X0, Y0);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias);

	}

	/**
	 * @param x
	 * @param y
	 * @param data
	 * @return
	 */
	private double getPlotValue(double x, double y, ContourPlotDataSeries data) {
		return data.getZValue(x, y);
	}

	/**
	 * @param value
	 * @return
	 */
	private int getColorByValue(double value) {
		Paint paint = this.getColorBar().getPaint(value);
		return ((Color) paint).getRGB();
	}

}
